using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ClosestPlayersShower : MonoBehaviour
{
	private struct PlayerDistance
	{
		public float distance;

		public PlayerBehavior player;
	}

	public int countPlayerShow = 2;

	private IEnumerator findNearestPlayer;

	private static int Compare(PlayerDistance a, PlayerDistance b)
	{
		if (a.distance < b.distance)
		{
			return -1;
		}
		if (a.distance > b.distance)
		{
			return 1;
		}
		return 0;
	}

	private IEnumerator FindNearestPlayer()
	{
		List<PlayerDistance> nearestPlayers = new List<PlayerDistance>();
		List<NJGMapItem> forceItems = new List<NJGMapItem>();
		List<NJGMapItem> forceItemsDelay = new List<NJGMapItem>();
		if (GameController.instance.curTypeGame == TypeGame.TitanfallV2 || GameController.instance.curTypeGame == TypeGame.WalkingRobots)
		{
			countPlayerShow = 20;
		}
		PlayerDistance playerDistance = default(PlayerDistance);
		while (true)
		{
			if ((bool)GameController.instance.playerScript)
			{
				PlayerBehavior mainPlayer = GameController.instance.playerScript;
				Vector3 pos = GameController.instance.playerScript.transform.position;
				Vector2 playerPos = new Vector2(pos.x, pos.z);
				nearestPlayers.Clear();
				foreach (PlayerBehavior player in GameController.instance.listPlayers)
				{
					if (!(player == null) && !(mainPlayer == player) && player.iconMiniMap.type != 29 && (player.transform.position.x != 0f || player.transform.position.z != 0f))
					{
						playerDistance.player = player;
						playerDistance.distance = (playerPos - new Vector2(player.transform.position.x, player.transform.position.z)).sqrMagnitude;
						nearestPlayers.Add(playerDistance);
					}
				}
				nearestPlayers.Sort(Compare);
				for (int j = 0; j < nearestPlayers.Count && j < countPlayerShow; j++)
				{
					if (nearestPlayers[j].player.currentCar != null && nearestPlayers[j].player.currentCar.iconMiniMap.type != 28 && nearestPlayers[j].player.currentCar.iconMiniMap.type != 40)
					{
						nearestPlayers[j].player.currentCar.iconMiniMap.enabled = true;
						nearestPlayers[j].player.currentCar.iconMiniMap.forceArrow = true;
					}
					else if (nearestPlayers[j].player.iconMiniMap.type != 28)
					{
						nearestPlayers[j].player.iconMiniMap.enabled = true;
						nearestPlayers[j].player.iconMiniMap.forceArrow = true;
					}
				}
				for (int i = countPlayerShow; i < nearestPlayers.Count; i++)
				{
					if (nearestPlayers[i].player.currentCar != null && nearestPlayers[i].player.currentCar.iconMiniMap.type != 40)
					{
						nearestPlayers[i].player.currentCar.iconMiniMap.forceArrow = false;
						nearestPlayers[i].player.currentCar.iconMiniMap.enabled = false;
					}
					else
					{
						nearestPlayers[i].player.iconMiniMap.forceArrow = false;
						nearestPlayers[i].player.iconMiniMap.enabled = false;
					}
				}
			}
			yield return new WaitForSeconds(0.5f);
		}
	}

	private void OnDisable()
	{
		if (findNearestPlayer != null)
		{
			StopCoroutine(findNearestPlayer);
		}
	}

	private void OnEnable()
	{
		if (settings.offlineMode || GameController.instance == null || GameController.instance.curTypeGame == TypeGame.race || GameController.instance.curTypeGame == TypeGame.RocketLeague)
		{
			base.enabled = false;
			return;
		}
		findNearestPlayer = FindNearestPlayer();
		StartCoroutine(findNearestPlayer);
	}
}
